package com.lxj.controller.sys;

import com.lxj.common.Result;
import com.lxj.domain.SysRole;
import com.lxj.dto.SysRoleDto;
import com.lxj.service.RoleService;
import com.lxj.vo.DTreeVo;
import com.lxj.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author lxj
 * @date 2021/6/16 16:00
 * @Description: 角色的控制器
 */
@RestController
@RequestMapping("/sysrole")
public class SysRoleController {

    @Autowired
    private RoleService roleService;
    
    /**
     * 新增用户的方法
     * @param dto
     * @return
     */
    @PostMapping("/add.do")
    public Result addRole(@RequestBody SysRoleDto dto){
        return roleService.addRole(dto);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @PostMapping("/delete.do/{id}")
    public Result deleteRoleById(@PathVariable("id") Integer id){
        return roleService.deleteRoleById(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete.do/{ids}")
    public Result batchDelete(@PathVariable("ids") Integer[] ids){
        return roleService.batchDelete(ids);
    }

    /**
     * 修改用户
     * @param dto
     * @return
     */
    @PostMapping("/update.do")
    public Result updateRole(@RequestBody SysRoleDto dto){
        return roleService.updateRole(dto);
    }
    
    /**
     * 分页查询
     * @param dto
     * @return
     */
    @GetMapping("/findByPage.do")
    public Result selectPage(SysRoleDto dto){
        PageInfo<SysRole> pageInfo = roleService.selectPage(dto);
        return new Result(pageInfo);
    }

    /**
     * 获取当前用户的角色
     * @param userId
     * @return
     */
    @RequestMapping("/getUserRole.do")
    public Result getUserRole(Integer userId) {
        // 我们得到了用户的id-->得到该用户的角色数据
        List<Integer> roleIds = roleService.getUserRoleIds(userId);
        List<SysRole> sysRoles = roleService.selectAllRoles();
        List<DTreeVo> treeVos = convertAndMapping(sysRoles, roleIds);
        return new Result(treeVos);
    }
    /**
     * 给用户分配角色
     * @param userId  用户id
     * @param roleIds 角色的集合
     * @return
     */
    @PostMapping("/assign.do")
    public Result assign(Integer userId, @RequestBody Integer[] roleIds) {
        roleService.assign(userId, roleIds);
        return new Result();
    }
    /**
     * @param sysRoles
     * @param roleIds
     * @return
     */
    private List<DTreeVo> convertAndMapping(List<SysRole> sysRoles, List<Integer> roleIds) {
        List<DTreeVo> dTreeVos = null;
        if (sysRoles != null && sysRoles.size() > 0) {
            dTreeVos = new ArrayList<DTreeVo>();
            for (SysRole sysRole : sysRoles) {
                DTreeVo dTreeVo = new DTreeVo();
                dTreeVo.setId(sysRole.getId());
                dTreeVo.setTitle(sysRole.getName());
                Map<String, Object> checkArr = new HashMap<>();
                if (roleIds.contains(sysRole.getId())) {
                    checkArr.put("type", 0);
                    checkArr.put("checked", 1);
                } else {
                    checkArr.put("type", 0);
                    checkArr.put("checked", 0);
                }
                dTreeVo.setCheckArr(checkArr);
                dTreeVos.add(dTreeVo);
            }
            return dTreeVos;
        }
        return Collections.emptyList();
    }
}
